import * as types from './types'
import echarts from 'echarts'
import { getOneStringDes } from '../../utils/sysMakeString'
import { getmakeStringDes } from './utils/makeString'
const mutations = {
  // 表格 排序 字段赋值
  [types.TABLE_SORTER_STATE_INIT] (state, { type, val }) {
    if (val.order === null) {
      state[type + 'Table'].allocation.sorter.field = ''
      state[type + 'Table'].allocation.sorter.order = ''
    } else {
      state[type + 'Table'].allocation.sorter.field = val.field
      state[type + 'Table'].allocation.sorter.order = val.order.replace('ing', '')
      state[type + 'Table'].allocation.pagination.current = 1
    }
  },
  // 表格 筛选 字段排序
  [types.TABLE_FILTER_STATE_INIT] (state, { type, val }) {
    let valArr = []
    for (let i of val.value) {
      valArr.push(i)
    }
    state[type + 'Table'].allocation.filters[val.key] = valArr
    state[type + 'Table'].allocation.pagination.current = 1
  },
  // 表格 分页-每页显示条数 字段排序
  [types.TABLE_PAGINATION_SIZE_STATE_INIT] (state, { type, val }) {
    state[type + 'Table'].allocation.pagination.pageSize = val.size
    state[type + 'Table'].allocation.pagination.current = 1
  },
  // 表格 分页-当前页数 字段排序
  [types.TABLE_PAGINATION_CURRENT_STATE_INIT] (state, { type, val }) {
    state[type + 'Table'].allocation.pagination.current = val.cur
  },
  // 初始化state数据
  [types.COMMIT_CLEAR_ALL_DATA] (state) {
    state.autoAnalyzeLoading = false
    state.personAnalyzeLoading = false
    state.AutoAttackStatistics = {}
    state.PerAttackStatistics = {}
    state.AutoAttackTable = {
      data: [],
      total: 0,
      allocation: {
        pagination: {
          current: 1, // 当前页码
          pageSize: 20 // 当前页显示的数据条数
        },
        filters: {
          athreaten_level: [],
          aconclusion: [],
          ahandle_status: []
        },
        search: {
          value: '' // 需要模糊搜索的值（搜索框）
        },
        sorter: {
          field: '', // 排序的字段
          order: '' // or "desc"升序还是降序
        }
      }
    }
    state.PerAttackTable = {
      data: [],
      total: 0,
      allocation: {
        pagination: {
          current: 1, // 当前页码
          pageSize: 20 // 当前页显示的数据条数
        },
        filters: {
          athreaten_level: [],
          aconclusion: [],
          ahandle_status: []
        },
        search: {
          value: '' // 需要模糊搜索的值（搜索框）
        },
        sorter: {
          field: '', // 排序的字段
          order: '' // or "desc"升序还是降序
        }
      }
    }
    state.ruleConfigs = []
    state.ruleConfigsCopy = []
    state.checkedLogData = {
      isLoading: false,
      data: [],
      total: 0,
      log_ids: [],
      allocation: {
        pagination: {
          current: 1, // 当前页码
          pageSize: 5 // 当前页显示的数据条数
        },
        filters: {
          alog_level: [],
          alog_type: []
        },
        sorter: {
          field: '', // 排序的字段
          order: '' // or "desc"升序还是降序
        }
      }
    }
  },
  [types.COMMIT_CLEAR_CHECKED_LOG_DATA] (state) {
    state.checkedLogData = {
      isLoading: false,
      data: [],
      total: 0,
      log_ids: [],
      allocation: {
        pagination: {
          current: 1, // 当前页码
          pageSize: 5 // 当前页显示的数据条数
        },
        filters: {
          alog_level: [],
          alog_type: []
        },
        sorter: {
          field: '', // 排序的字段
          order: '' // or "desc"升序还是降序
        }
      }
    }
  },
  // 自动|人工分析--表格加载
  [types.COMMIT_LOADING_CHANGE] (state, { type, val }) {
    state[`${type}AnalyzeLoading`] = val
  },
  // 自动|人工分析--统计区数据
  [types.COMMIT_ATTACK_STATISTICAL] (state, { data, type }) {
    state[type + 'Statistics'] = data
  },
  // 自动|人工分析--echarts
  [types.COMMIT_ATTACK_CHARTS] (state, { data, type }) {
    if (type === 'auto') {
      let analyzeArr = [
        { name: '未分析', value: state.AutoAttackStatistics.analysisStatistical.underway || 0 },
        { name: '已分析', value: state.AutoAttackStatistics.analysisStatistical.finish || 0 }
      ]
      let ruleArr = [
        { name: '反复出现', value: state.AutoAttackStatistics.ruleStatistical.recurring || 0 },
        { name: '长期未处置', value: state.AutoAttackStatistics.ruleStatistical.untreated || 0 },
        // { name: '特定行为', value: state.AutoAttackStatistics.ruleStatistical.behavior || 0 },
        // { name: '特定类型', value: state.AutoAttackStatistics.ruleStatistical.type || 0 },
        { name: '其他', value: state.AutoAttackStatistics.ruleStatistical.other || 0 }
      ]
      if (document.getElementById('attackRiskTrend')) {
        let attackRiskTrend = echarts.init(document.getElementById('attackRiskTrend'))
        let attackRiskOptions = {
          xAxis: {
            type: 'category',
            data: data.waves.length > 0 ? data.waves.map(val => val.r_time.substring(0, 10)) : [],
            boundaryGap: false,
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#1f92ef'
              }
            }
          },
          yAxis: {
            type: 'value',
            show: false
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'none',
              label: {
                backgroundColor: '#6a7985'
              }
            },
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          series: [{
            smooth: true,
            name: '攻击事件总数',
            symbol: 'none',
            data: data.waves.length > 0 ? data.waves.map(val => val.all_risk_cnt) : [],
            type: 'line',
            areaStyle: {
              color: '#8fc8f7'
            },
            lineStyle: {
              width: 1,
              color: '#8fc8f7'
            },
            itemStyle: {
              color: '#8fc8f7'
            }
          }, {
            smooth: true,
            name: '已分析数量',
            symbol: 'none',
            data: data.waves.length > 0 ? data.waves.map(val => val.analysis_risk_cnt) : [],
            type: 'line',
            areaStyle: {
              color: '#1f92ef'
            },
            lineStyle: {
              width: 1,
              color: '#1f92ef'
            },
            itemStyle: {
              color: '#1f92ef'
            }
          }
          ],
          grid: {
            top: 5,
            bottom: 0,
            left: 0,
            right: 0
          }
        }
        attackRiskTrend.setOption(attackRiskOptions)
      }
      if (document.getElementById('attackRuleChart')) {
        let attackRuleChart = echarts.init(document.getElementById('attackRuleChart'))
        let ruleOptions = {
          tooltip: {
            trigger: 'item',
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          legend: {
            orient: 'vertical',
            left: '240',
            top: '0',
            itemWidth: 10,
            itemHeight: 10,
            icon: 'circle',
            textStyle: {
              color: '#5c6781',
              fontSize: 12,
              fontFamily: 'SimSun'
            },
            formatter: function (name) {
              if (name === '反复出现') {
                return `反复出现:${data.ruleStatistical.recurring}`
              } else if (name === '长期未处置') {
                return `长期未处置:${data.ruleStatistical.untreated}`
              } else if (name === '特定行为') {
                return `特定行为:${data.ruleStatistical.behavior}`
              } else if (name === '特定类型') {
                return `特定类型:${data.ruleStatistical.type}`
              } else if (name === '其他') {
                return `其他:${data.ruleStatistical.other}`
              }
            }
          },
          color: ['#ffd140', '#1f92ef', '#53cb7b', '#fe9833'],
          series: [{
            type: 'pie',
            center: ['40%', '43%'],
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                formatter: '{c}',
                position: 'center'
              },
              emphasis: {
                show: true,
                color: '#5c6781',
                textStyle: {
                  fontSize: '12',
                  fontFamily: 'SimSun'
                }
              }
            },
            data: ruleArr
          }]
        }
        attackRuleChart.setOption(ruleOptions)
      }
      if (document.getElementById('attackAnalyzeChart')) {
        let attackAnalyzeChart = echarts.init(document.getElementById('attackAnalyzeChart'))
        let analyzeOptions = {
          tooltip: {
            trigger: 'item',
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          legend: {
            orient: 'vertical',
            left: '240',
            top: '10',
            itemWidth: 10,
            itemHeight: 10,
            icon: 'circle',
            textStyle: {
              color: '#5c6781',
              fontSize: 12,
              fontFamily: 'SimSun'
            },
            formatter: function (name) {
              if (name === '未分析') {
                return `未分析:${data.analysisStatistical.underway}`
              } else if (name === '已分析') {
                return `已分析:${data.analysisStatistical.finish}`
              }
            }
          },
          color: ['#ffd140', '#1f92ef'],
          series: [{
            type: 'pie',
            center: ['40%', '43%'],
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                formatter: '{c}',
                position: 'center'
              },
              emphasis: {
                show: true,
                color: '#5c6781',
                textStyle: {
                  fontSize: '16'
                }
              }
            },
            data: analyzeArr
          }]
        }
        attackAnalyzeChart.setOption(analyzeOptions)
      }
    } else if (type === 'per') {
      let analyzeArr = [
        { name: '未分析', value: state.PerAttackStatistics.analysisStatistical.underway || 0 },
        { name: '已分析', value: state.PerAttackStatistics.analysisStatistical.finish || 0 }
      ]
      let riskLevel = [
        { name: '低危', value: state.PerAttackStatistics.risk_level.level1 || 0 },
        { name: '中危', value: state.PerAttackStatistics.risk_level.level2 || 0 },
        { name: '高危', value: state.PerAttackStatistics.risk_level.level3 || 0 },
        { name: '严重', value: state.PerAttackStatistics.risk_level.level4 || 0 }
      ]
      if (document.getElementById('perAttackRiskTrend')) {
        let attackRiskTrend = echarts.init(document.getElementById('perAttackRiskTrend'))
        let attackRiskOptions = {
          xAxis: {
            type: 'category',
            data: data.waves.length > 0 ? data.waves.map(val => val.r_time.substring(0, 10)) : [],
            boundaryGap: false,
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#1f92ef'
              }
            }
          },
          yAxis: {
            type: 'value',
            show: false
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'none',
              label: {
                backgroundColor: '#6a7985'
              }
            },
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          series: [{
            smooth: true,
            name: '攻击事件总数',
            symbol: 'none',
            data: data.waves.length > 0 ? data.waves.map(val => val.all_risk_cnt) : [],
            type: 'line',
            areaStyle: {
              color: '#8fc8f7'
            },
            lineStyle: {
              width: 1,
              color: '#8fc8f7'
            },
            itemStyle: {
              color: '#8fc8f7'
            }
          }, {
            smooth: true,
            name: '已分析数量',
            symbol: 'none',
            data: data.waves.length > 0 ? data.waves.map(val => val.analysis_risk_cnt) : [],
            type: 'line',
            areaStyle: {
              color: '#1f92ef'
            },
            lineStyle: {
              width: 1,
              color: '#1f92ef'
            },
            itemStyle: {
              color: '#1f92ef'
            }
          }
          ],
          grid: {
            top: 5,
            bottom: 0,
            left: 0,
            right: 0
          }
        }
        attackRiskTrend.setOption(attackRiskOptions)
      }
      if (document.getElementById('perAttackLevelChart')) {
        let attackRuleChart = echarts.init(document.getElementById('perAttackLevelChart'))
        let ruleOptions = {
          tooltip: {
            trigger: 'item',
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          legend: {
            orient: 'vertical',
            left: '240',
            top: '0',
            itemWidth: 10,
            itemHeight: 10,
            icon: 'circle',
            textStyle: {
              color: '#5c6781',
              fontSize: 12,
              fontFamily: 'SimSun'
            },
            formatter: function (name) {
              if (name === '低危') {
                return `低危:${data.risk_level.level1}`
              } else if (name === '中危') {
                return `中危:${data.risk_level.level2}`
              } else if (name === '高危') {
                return `高危:${data.risk_level.level3}`
              } else if (name === '严重') {
                return `严重:${data.risk_level.level4}`
              }
            }
          },
          color: ['#ffc833', '#ff9933', '#ff6633', '#ff3333'],
          series: [{
            type: 'pie',
            center: ['40%', '43%'],
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                formatter: '{c}',
                position: 'center'
              },
              emphasis: {
                show: true,
                color: '#5c6781',
                textStyle: {
                  fontSize: '12',
                  fontFamily: 'SimSun'
                }
              }
            },
            data: riskLevel
          }]
        }
        attackRuleChart.setOption(ruleOptions)
      }
      if (document.getElementById('perAttackAnalyzeChart')) {
        let attackAnalyzeChart = echarts.init(document.getElementById('perAttackAnalyzeChart'))
        let analyzeOptions = {
          tooltip: {
            trigger: 'item',
            textStyle: {
              color: '#fff',
              fontSize: 12,
              fontFamily: 'SimSun'
            }
          },
          legend: {
            orient: 'vertical',
            left: '240',
            top: '10',
            itemWidth: 10,
            itemHeight: 10,
            icon: 'circle',
            textStyle: {
              color: '#5c6781',
              fontSize: 12,
              fontFamily: 'SimSun'
            },
            formatter: function (name) {
              if (name === '未分析') {
                return `未分析:${data.analysisStatistical.underway}`
              } else if (name === '已分析') {
                return `已分析:${data.analysisStatistical.finish}`
              }
            }
          },
          color: ['#ffd140', '#1f92ef'],
          series: [{
            type: 'pie',
            center: ['40%', '43%'],
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                formatter: '{c}',
                position: 'center'
              },
              emphasis: {
                show: true,
                color: '#5c6781',
                textStyle: {
                  fontSize: '16'
                }
              }
            },
            data: analyzeArr
          }]
        }
        attackAnalyzeChart.setOption(analyzeOptions)
      }
    }
  },
  // 自动|人工分析--表格数据
  [types.COMMIT_ATTACK_TABLEDATA] (state, { data, type }) {
    state[`${type}Table`].data = data.dataSource
    state[`${type}Table`].total = data.total
  },
  // 自动分析--规则配置
  [types.COMMIT_RULES_CONFIG_INIT] (state, { data }) {
    state.ruleConfigs = data.dataSource
    state.ruleConfigsCopy = JSON.parse(JSON.stringify(data.dataSource))
  },
  // 深度分析--事件信息&影响分析
  [types.COMMIT_DEPTH_ANALYSIS_EVENT_INFO] (state, { registData, data }) {
    for (let key in data.dataSource) {
      registData.depthAnalysisEventInfo[key] = data.dataSource[key]
    }
  },
  // 深度分析--分析结论统计计算
  [types.COMMIT_DEPTH_ANALYSIS_STATISTICAL_CONCLUSION] (state, { registData }) {
    registData.affectAssets = {
      affectUnit: 0,
      affectSystem: 0,
      affectNode: 0
    }
    for (let index in registData.depthAnalysisEventInfo.impactAnalysis) {
      registData.affectAssets.affectUnit += Number(registData.depthAnalysisEventInfo.impactAnalysis[index].affect_unit)
      registData.affectAssets.affectSystem += Number(registData.depthAnalysisEventInfo.impactAnalysis[index].affect_system)
      registData.affectAssets.affectNode += Number(registData.depthAnalysisEventInfo.impactAnalysis[index].affect_node)
    }
    for (let index in registData.depthAnalysisEventInfo.potentialImpacts) {
      registData.affectAssets.affectUnit += Number(registData.depthAnalysisEventInfo.potentialImpacts[index].affect_unit)
      registData.affectAssets.affectSystem += Number(registData.depthAnalysisEventInfo.potentialImpacts[index].affect_system)
      registData.affectAssets.affectNode += Number(registData.depthAnalysisEventInfo.potentialImpacts[index].affect_node)
    }
  },
  // 深度分析--添加--搜索数据
  [types.COMMIT_GET_POTENTIAL_LOG] (state, { registData, data }) {
    for (let log in data.esData.hits.hits) {
      let el = data.esData.hits.hits[log]
      el._source.describes = getOneStringDes(el._source)
      el._source.log_type_cn = getOneStringDes(el._source, 'name')
      // el._source.describes = '描述信息'
    }
    registData.addElementTable.tableData = data.esData.hits.hits
    registData.addElementTable.total = data.esData.hits.total
  },
  // 深度分析--编辑--获取选中日志信息
  [types.COMMIT_GET_CHECKED_LOG] (state, { data }) {
    for (let index in data.dataSource) {
      let log = data.dataSource[index]
      log.describes = getmakeStringDes(log)
      log.log_type_cn = getmakeStringDes(log, 'name')
    }
    state.checkedLogData.data = data.dataSource
    state.checkedLogData.total = data.total
    if (data.log_ids) {
      state.checkedLogData.log_ids = data.log_ids.split('|')
    } else {
      state.checkedLogData.log_ids = []
    }
  },
  // 更新时间选择器时间
  [types.SET_DAYRANGE] (state, data) {
    state.dayRange = data
  }
}

export default mutations
